home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / aspisrc.zip / GETOLDOP.C < prev    next >
C/C++ Source or Header  |  1992-01-26  |  2KB  |  89 lines

  1. /* Replacement for getopt() that can be used by tar.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Plug-compatible replacement for getopt() for parsing tar-like
  22.  * arguments.  If the first argument begins with "-", it uses getopt;
  23.  * otherwise, it uses the old rules used by tar, dump, and ps.
  24.  *
  25.  * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu)
  26.  *
  27.  * @(#)getoldopt.c 1.4 2/4/86 - gnu
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include "getopt.h"
  32.  
  33. int
  34. getoldopt(argc, argv, optstring, long_options, opt_index)
  35.     int    argc;
  36.     char    **argv;
  37.     char    *optstring;
  38.     struct option *long_options;
  39.     int     *opt_index;
  40. {
  41.     extern char    *optarg;    /* Points to next arg */
  42.     extern int    optind;        /* Global argv index */
  43.     static char    *key;        /* Points to next keyletter */
  44.     static char    use_getopt;    /* !=0 if argv[1][0] was '-' */
  45.     extern char    *index();
  46.     char        c;
  47.     char        *place;
  48.  
  49.     optarg = NULL;
  50.     
  51.     if (key == NULL) {        /* First time */
  52.         if (argc < 2) return EOF;
  53.         key = argv[1];
  54.         if ((*key == '-') || (*key == '+'))
  55.             use_getopt++;
  56.         else
  57.             optind = 2;
  58.     }
  59.  
  60.     if (use_getopt)
  61.         return getopt_long(argc, argv, optstring, 
  62.                    long_options, opt_index);
  63.  
  64.     c = *key++;
  65.     if (c == '\0') {
  66.         key--;
  67.         return EOF;
  68.     }
  69.     place = index(optstring, c);
  70.  
  71.     if (place == NULL || c == ':') {
  72.         msg("unknown option %c", c);
  73.         return('?');
  74.     }
  75.  
  76.     place++;
  77.     if (*place == ':') {
  78.         if (optind < argc) {
  79.             optarg = argv[optind];
  80.             optind++;
  81.         } else {
  82.             msg("%c argument missing", c);
  83.             return('?');
  84.         }
  85.     }
  86.  
  87.     return(c);
  88. }
  89.